
import BaseMixin from '@/views/mixin.vue'
import { HttpShipperManageList } from '@/service/api/views'
import { Button, Dropdown, DropdownMenu, Icon } from 'iview';
import { mapState } from 'vuex'

import compAdd from './add';
import compSearch from './search';
import compControl from './control';

import { FSAPI } from '@/utils/assist';

import compsoUpload from './soloupload';
export default {
    mixins: [BaseMixin],
    components: {
        compSearch,
        compAdd,
        compControl,
        compsoUpload
    },
    data() {
        return {
            title: "发货地址管理"
        };

    },
    methods: {
        httpList: HttpShipperManageList,
        handleOperate(id, type) {
            this.operate = {};
            switch (type) {
                case 'add':
                    this.operate = {
                        type, id,
                        show: true,
                        ismanage:1,
                        component: "compAdd",
                        title: "添加发货地址",
                        width: 660
                    }
                    break;
                case 'query':
                    this.$router.push({ name: 'shipperDetail', params: { id }, query: { type, ismanage:1 } });
                    break;
                case 'edit':
                    this.$router.push({ name: 'shipperDetail', params: { id }, query: { type, ismanage:1 } });
                    break;
                case 'delete':
                    this.showDel = true;
                    this.operate = {
                        id, type,
                    };
                    break;
                case 'upload':
                    this.operate = {
                        show: true,
                        title: '导入数据',
                        // data: this.formData,
                        actionurl: "/api/jwt/spl/shipper/uploadByManage",
                        name: "file",
                        component: "compsoUpload",
                        downrul: '/api/jwt/spl/shipper/downloadTemplate',
                        formdata: [
                            {
                                label: '组织',
                                tag: "vSelect",
                                key:'organizationId',
                                options:this.manageorglist.map(item=>{
                                    return {
                                        label:item.name,
                                        value:item.id
                                    }
                                }),
                                props:{
                                    filterable:true,
                                },
                                labelWidth: 90,
                                span:24,
                            },
                        ],
                    }
                    break;
                case 'download': this.download('/api/jwt/spl/shipper/exportByManage');
                    break;
                case 'control':
                    this.operate = {
                        type, 
                        id,
                        show: true,
                        ismanage:1,
                        component: "compControl",
                        title: "控量",
                        width: 450
                    }
                    break;
            }
        },
        download(url) {
            FSAPI.download(global.APIURL_HOST + url, this.formData)
        },
        async batchOpearte() {
            let request, { type, id } = this.operate, params = null;
            switch (type) {
                case 'delete': request = HttpDeleteReceiver; params = { id }; break;
                default: request = function () { }; break;
            }
            request(params).then(res => {
                this.updateList();
                this.$Message.success(res.message);
            }, err => {
                this.$Message.error(err.message);
            }).finally(result => {
                this.showDel = false;
                this.operate = {};
            })
        },

    },
    computed: {
        ...mapState(['systemlist','manageorglist']),
        columns() {
            return [
                {
                    title: "NO",
                    key: "NO",
                    width: 60,
                    type: "index",
                    align: 'center',
                }, {
                    title: "操作",
                    width: 170,
                    render: (h, { row }) => {
                        const id = row.id;
                        return <div>
                            <Button type="primary" size="small" class='margin-right-5' onClick={this.handleOperate.bind(this, id, 'query')}>查看</Button>
                            <Button type="success" size="small" class='margin-right-5' onClick={this.handleOperate.bind(this, id, 'edit')}>编辑</Button>
                            <Button type="warning" size="small" onClick={this.handleOperate.bind(this, {id,labellist:row.labelLimit}, 'control')}>控量</Button>
                        </div>
                    }
                }, 
                {
                    title: "组织",
                    key: "organizationId",
                    minWidth: 120,
                    sortable: true,
                    render: (h, { row }) => {
                        const id = row.organizationId,
                            data = this.systemlist.find((item) => {
                                return item.id === id
                            })||{};
                        return <div>{data.name}</div>
                    }
                },
                {
                    title: "发货方名称",
                    key: "contactName",
                    minWidth: 120,
                    sortable: true,
                },{
                    title: "发货方CODE",
                    key: "code",
                    minWidth: 120,
                    sortable: true,
                }, {
                    title: "发货方联系方式",
                    key: "contactPhone",
                    minWidth: 120,
                }, {
                    title: "发货方地址",
                    key: "countrycode",
                    minWidth: 120,
                    render(h, { row }) {
                        let address = `${row.countrycode} ${row.state}  ${row.city}  ${row.addressLine1 || ''}`;
                        return h('span', address)
                    }
                }, {
                    title: "状态",
                    minWidth: 100,
                    key: "active",
                    render: (h, { row }) => {
                        return h('span', row.active === 1 ? '启用' : '禁用')
                    }
                }, {
                    title: "备注",
                    key: "remark",
                    minWidth: 120,
                }, {
                    title: "创建人",
                    key: "createdName",
                    minWidth: 120,
                }, {
                    title: "创建时间",
                    minWidth: 120,
                    key: "createdAt",
                    sortable: true,
                }]
        },

    }
};




